<template>
  <div>
    <div class="default-main ba-table-box">
      <!-- <el-alert
        class="ba-table-alert"
        v-if="baTable.table.remark"
        :title="baTable.table.remark"
        type="info"
        show-icon
      /> -->
      <!-- <TableHeader
        :buttons="['refresh', 'columnDisplay']"
        :quick-search-placeholder="placeholder"
        @action="baTable.onTableHeaderAction"
      /> -->
      <Table
        ref="tableRef"
        :pagination="false"
        @action="baTable.onTableAction"
      />
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, provide, onMounted, inject, markRaw, reactive } from "vue";
import baTableClass from "@/utils/baTable";
import TableHeader from "@/components/table/header/index.vue";
import { baTableApi, operatorShowOff } from "@/utils/common"; // 导入表格api方法生成器
import Table from "@/components/table/index.vue";
import { useNavTabs } from "@/store/navTabs";
import TagVue from "./tag.vue";
import { getDictData } from "@/utils/common";
import { useRoute } from "vue-router";
import useCurrentInstance from "@/utils/useCurrentInstance";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import { BomMRoute } from "@/api/backend/index";

const plan = inject("plan") as any;
const dictionary = inject("dictionary") as any;
const placeholder = ref<string>("模糊搜索");
const tableRef = ref();
const route = useRoute();
const navTabs = useNavTabs();
const { proxy } = useCurrentInstance();

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};

const baTable = new baTableClass(
  new baTableApi(BomMRoute), //this.api = api部门管理
  {
    pk: "id",
    column: [
      {
        show: filedShowOff("materialCode"),
        label: "物料编码",
        prop: "materialCode",
        align: "left",
      },
      {
        show: filedShowOff("process"),
        label: "工序号",
        prop: "process",
        align: "left",
      },
      {
        show: filedShowOff("processName"),
        label: "工序名称",
        prop: "processName",
        align: "left",
      },
      {
        show: filedShowOff("workshopCode"),
        label: "车间代码",
        prop: "workshopCode",
        align: "left",
      },
      {
        show: filedShowOff("workshopName"),
        label: "车间名称",
        prop: "workshopName",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("prepareTime"),
        label: "单件额定准备时间",
        prop: "prepareTime",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("productTime"),
        label: "单件额定加工时间",
        prop: "productTime",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("equipmentCode"),
        label: "设备编号",
        prop: "equipmentCode",
        align: "left",
        operator: false,
      },
    ],
    extend: {
      // maxHeight: 500, //表格最大高度为300
    },
    filter: {
      materialCode: route.query.materialCode,
      materialType: route.query.materialType,
    },
    acceptQuery: false, //不接受url的query参数并自动触发通用搜索
  },
  {},
  {
    // getList: () => {
    //   plan
    //     .getPlanProcessList({ id: plan.planId })
    //     .then((res: any) => {
    //       baTable.table.data = res.rows;
    //       console.log("工艺路线", res, baTable.table.data);
    //     })
    //     .catch((err: any) => {});
    //   return false;
    // },
  },
  {}
);

//获取计工艺路线的是否暂停的字典
const getMenuStatus = () => {
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["isSuspend"])
  ) {
    dictionary.getDictInfo("sys_plan_suspend").then((res: any) => {
      dictionary.moreTags["isSuspend"] = getDictData(res);
      console.log("获取是否暂停的字典", dictionary.moreTags);
    });
  }
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["priority"])
  ) {
    dictionary.getDictInfo("sys_plan_priority").then((res: any) => {
      dictionary.moreTags["priority"] = getDictData(res);
      console.log("获取优先级的字典", dictionary.moreTags);
    });
  }
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["otherUnit"])
  ) {
    dictionary.getDictInfo("sys_process_otherunit").then((res: any) => {
      dictionary.moreTags["otherUnit"] = getDictData(res);
      console.log("获取是否外协的字典", dictionary.moreTags);
    });
  }
};
getMenuStatus();

plan.$subscribe((state: any) => {
  console.log("plan中数据变动", state.planId);
});
//接收来自

onMounted(() => {
  //全局事件总线
  //将table的ref给到table类
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(BomMRoute);
  /**
   * 表格内的按钮响应
   * @param name 按钮name
   * @param row 被操作行数据
   */
  proxy.eventBus.on(
    "onTableButtonClick",
    (data: { name: string; row: TableRow }) => {}
  );
});

// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
provide("dictionary", dictionary);
</script>
<style lang="scss" scoped>
.default-main {
  margin: 0px !important;
  margin-top: 5px !important;
}
</style>
